package cn.wolfcode.trip.admin;

import cn.wolfcode.trip.core.util.RequriedLogin;
import cn.wolfcode.trip.core.util.UploadUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 图片资源控制器
 */
@RestController
@RequestMapping("images")
public class ImageController {

    /**
     * 上传图片接口
     * 1.资源路径 /images
     * 2.请求动作 post
     * 3.请求参数 file
     * 4.返回结果 Map
     */
    @PostMapping
    @RequriedLogin //必须登录后才能访问
    public Map upload(MultipartFile upload)  {
        HashMap<String, Object> map = new HashMap<>();
        try {
            String url = UploadUtil.uploadAli(upload);
            map.put("uploaded",1);
            map.put("url",url);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("uploaded",0);
            map.put("msg","上传失败!");
        }
        return map;
    }
}
